package batchprocessor;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Command {

    // Private variables
    String id;
    String path;
    String inFile;
    String outFile;
    String commands;
    Process process;

    // Command Class constructor
    public Command(String n_id, String n_path, String n_inFile,
            String n_outFile, String n_commands) {

        id = n_id;
        path = n_path;
        inFile = n_inFile;
        outFile = n_outFile;
        commands = n_commands;
    }

    // Default Constructor
    public Command() {

        // Initialize private variables
        id = "";
        path = "";
        inFile = "";
        outFile = "";
        commands = "";
    }

    public String getId() {
        return id;
    }

    public String getPath() {
        return path;
    }

    public String getInFile() {
        return inFile;
    }

    public String getOutFile() {
        return outFile;
    }

    public String getCommands() {
        return commands;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public void setInFile(String inFile) {
        this.inFile = inFile;
    }

    public void setOutFile(String outFile) {
        this.outFile = outFile;
    }

    public void setCommands(String commands) {
        this.commands = commands;
    }

    // Start the process for this command
    public void start(String dir) throws ProcessException {
        System.out.println("Starting command " + id);

        ProcessBuilder builder = new ProcessBuilder();

        // Build the command to give the builder
        List<String> command = new ArrayList<>();
        command.add(path);

        if (!"Null".equals(commands)) {
            String[] command_strings = commands.split("\\s+");
            command.addAll(Arrays.asList(command_strings));
        }

        // Setup the builder
        builder.command(command);
        builder.directory(new File(dir));
        if (!"".equals(inFile)) {
            //Make sure file exists
            File file = new File(dir + "/" + inFile);
            if(file.exists()){
                builder.redirectInput(new File(dir + "/" + inFile));
            } else {
                System.out.println("Input file not found");
                throw new ProcessException("Input file not found");
            }
        }
        
        if (!"".equals(outFile)) {
            builder.redirectOutput(new File(dir + "/" + outFile));
            
        }

        builder.redirectError(new File(dir + "/" + "error.txt"));

        // Start the process
        try {
            process = builder.start();
        } catch (IOException ex) {
            // TODO Exception Handling
            System.out.println("IO Exception in command");
            throw new ProcessException("IO Exception in command");
        }
    }

    // Wait for this process to complete
    public void finish() throws ProcessException{
        try {
            process.waitFor();
        } catch (InterruptedException ex) {
            System.out.println("Process interrupted");
            throw new ProcessException("Process interrupted");
        }

        System.out.println("Command " + id + " complete");
    }
}
